﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace LockTest
{
    public partial class Form1 : Form
    {
        delegate void MessageHandler(string msg);


        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            LockerTest test = new LockerTest("LockTest 1");
            test.MessageEvent += new LockerTest.MessageEventHandler(MessageCallBack);
            listBox1.Items.Clear();
            for (int i = 0; i <= 2; i++)
            {
                Thread a = new Thread(new ThreadStart(test.AddNum1));
                a.Name = i.ToString();
                a.Start();
            }
        }

        public void MessageCallBack(object sender, MyEventArgs e)
        {
            MessageHandler handler = new MessageHandler(ShowMessage);
            this.Invoke(handler, new object[] { e.Message });
        }

        public void ShowMessage(string msg)
        {
            this.listBox1.Items.Add(msg);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            LockerTest test = new LockerTest("LockTest 1");
            test.MessageEvent += new LockerTest.MessageEventHandler(MessageCallBack);
            listBox1.Items.Clear();
            for (int i = 0; i <= 2; i++)
            {
                Thread a = new Thread(new ThreadStart(test.AddNum2));
                a.Name = i.ToString();
                a.Start();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            for (int i = 0; i <= 2; i++)
            {
                LockerTest test = new LockerTest("LockTest " + i.ToString());
                test.MessageEvent += new LockerTest.MessageEventHandler(MessageCallBack);
                Thread a = new Thread(new ThreadStart(test.AddNum1));
                a.Name = i.ToString();
                a.Start();
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            for (int i = 0; i <= 2; i++)
            {
                LockerTest test = new LockerTest("LockTest " + i.ToString());
                test.MessageEvent += new LockerTest.MessageEventHandler(MessageCallBack);
                Thread a = new Thread(new ThreadStart(test.AddNum2));
                a.Name = i.ToString();
                a.Start();
            }
        }
    }
}
